package com.ccit.springbootlaw.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JWTutil {
    public static final String SECRET = "springbootLaw";

    /**
     * 生成token
     */
    public static String generateToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("user", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))
                .sign(Algorithm.HMAC256(SECRET));
    }

    /**
     * 验证token
     */
    public static Map<String, Object> verifyToken(String token) {
        return JWT.require(Algorithm.HMAC256(SECRET))
                .build().verify(token)
                .getClaim("user")
                .asMap();
    }
}
